﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.BasicSet.Impl;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.BasicSet.HandSetType;
using WaterRevenueMS.Core.Service.BasicSet;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.BasicSet;
using WaterRevenueMS.WebUI.Extension.MvcHelper;


namespace WaterRevenueMS.WebUI.Controllers
{
   
    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class HandSetTypeController : AdminController
    {
        [Import]
        public IHandSetTypeService HandSetTypeService { get; set; }

        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var model = new HandSetTypeModel();
            return View(model);
        }

        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = HandSetTypeService.HandSetTypes.Count(t => t.IsDeleted == false);

            var expr = BuildSearchCriteria();

            var filterResult = HandSetTypeService.HandSetTypes.Where(expr).Select(t => new HandSetTypeModel
            {
                Id = t.Id,
                Shou_CQXH = t.Shou_CQXH,
                Shou_CQLX = t.Shou_CQLX,
                Tong_XDK = t.Tong_XDK,
                Bo_TL = t.Bo_TL,
                Jiao_YFS = t.Jiao_YFS,
                Shu_JW = t.Shu_JW,
                Ting_ZW = t.Ting_ZW,
                Wo_SXY = t.Wo_SXY,
                OrderNo = t.OrderNo

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 

                            c.Shou_CQLX,
                            c.Shou_CQXHXS,
                            c.Tong_XDKXS, 
                    
                            c.Bo_TL.ToString(),
                            c.Jiao_YFSXS.ToString(),
                            c.Shu_JW.ToString(),
                            c.Ting_ZW.ToString(),
                            c.Wo_SXY.ToString(),
                            c.OrderNo.ToString(),
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Create()
        {
            var model = new HandSetTypeModel();

            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(HandSetTypeModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = HandSetTypeService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new HandSetTypeModel();
            var entity = HandSetTypeService.HandSetTypes.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new HandSetTypeModel
                {
                    Id = entity.Id,
                    Shou_CQLX= entity.Shou_CQLX,
                    Shou_CQXH = entity.Shou_CQXH,
                    Tong_XDK = entity.Tong_XDK,
                    Bo_TL = entity.Bo_TL,
                    Jiao_YFS = entity.Jiao_YFS,
                    Shu_JW = entity.Shu_JW,
                    Ting_ZW = entity.Ting_ZW,
                    Wo_SXY = entity.Wo_SXY,
                    OrderNo = entity.OrderNo


                };
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Edit(HandSetTypeModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = HandSetTypeService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }

            }
            else
            {
                return PartialView(model);
            }
        }


        public ActionResult Delete(int Id)
        {
            OperationResult result = HandSetTypeService.Delete(Id);
            return Json(result);
        }


        private Expression<Func<HandSetType, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<HandSetType> bulider = new DynamicLambda<HandSetType>();
            Expression<Func<HandSetType, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Shou_CQLX"]))
            {
                var data = Request["Shou_CQLX"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Shou_CQLX.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Shou_CQXH"]))
            {
                var data = Request["Shou_CQXH"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Shou_CQXH.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            if (!string.IsNullOrEmpty(Request["Tong_XDK"]))
            {
                var data = Request["Tong_XDK"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Tong_XDK.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Bo_TL"]))
            {
                var data = Request["Bo_TL"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Bo_TL.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Jiao_YFS"]))
            {
                var data = Request["Jiao_YFS"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Jiao_YFS.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Shu_JW"]))
            {
                var data = Request["Shu_JW"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Shu_JW.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            if (!string.IsNullOrEmpty(Request["Ting_ZW"]))
            {
                var data = Request["Ting_ZW"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Ting_ZW.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            if (!string.IsNullOrEmpty(Request["Wo_SXY"]))
            {
                var data = Request["Wo_SXY"].Trim();
                Expression<Func<HandSetType, Boolean>> tmp = t => t.Wo_SXY.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            Expression<Func<HandSetType, Boolean>> tmpSolid = t => t.IsDeleted == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }

    }
}

   
